#include<bits/stdc++.h>
using namespace std;
const int N=200005;

int n;
int a[N];
int depth[N];
bool visit[N];
bool visitc[N];

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    memset(visit,0,sizeof(visit));
    memset(depth,0,sizeof(depth));
    int res = INT_MAX;
    int pre=1;
    while(1){
        stack<int> sta;
        memset(visitc,0,sizeof(visitc));
        for(int i=pre+1;i<=n;i++){
            if(!visit[i]){
                depth[i]=1;
                sta.push(i);
                visit[i]=visitc[i]=true;
                pre=i;
                break;
            }
        }
        if(sta.empty()) break;
        while(1){
            int s=sta.top();
            sta.pop();
            int t=a[s];
            if(visitc[t]){
                res = min(res,depth[s]+1-depth[t]);
                break;
            }
            else if(visit[t]){
                break;
            }
            else{
                sta.push(t);
                depth[t]=depth[s]+1;
                visit[t]=visitc[t]=true;
            }
        }

    }
    printf("%d",res);
    return 0;
}